package com.kmzx.one.project.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kmzx.one.project.entity.RankTab;
import com.kmzx.one.project.mapper.ProductMapper;
import com.kmzx.one.project.mapper.UserMessageMapper;
import com.kmzx.one.project.others.dto.FriendPic;
import com.kmzx.one.project.others.dto.MemberActionInfo;
import com.kmzx.one.project.others.dto.MemberCenter;
import com.kmzx.one.project.others.dto.UserMsgInfo;
import com.kmzx.one.project.service.UserMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Project one
 * @Author: carol
 * @Date: 2020/9/3-10:31
 */
@Component
public class UserMessageServiceImpl implements UserMessageService {

    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    UserMessageMapper userMessageMapper;

    @Autowired
    ProductMapper productMapper;

    public int checkUserMsgHas(String userId) {
        logger.info("检测用户是否有未读消息");
        int hasUserMsg = userMessageMapper.checkHasUserMsg(userId);
        return hasUserMsg;
    }

    public void readingUserMsg(String userId, String id) {
        logger.info("用户消费消息,{}", userId);
        Date date = new Date();
        userMessageMapper.readMessageUser(id, userId, date);
    }

    public List<UserMsgInfo> findUserMsgInfo(String userId, Integer pageNum, Integer pageSize) {
        logger.info("读取用户的消息列表,{}", userId);
        PageHelper.startPage(pageNum, pageSize, true);
        Page<UserMsgInfo> userMsgInfo = userMessageMapper.findUserMsgInfo(userId);
        if (userMsgInfo != null && userMsgInfo.size() > 0) {
            logger.info("读取用户的消息列表,详情,{}", userMsgInfo);
            List<UserMsgInfo> result = userMsgInfo.getResult();
            return result;
        } else {
            return Collections.emptyList();
        }
    }

    @Override
    public void readMessageUserAll(String userId) {
        userMessageMapper.readMessageUserAll(userId,new Date());
    }

    @Override
    public List<FriendPic> findFriendPic() {
        return userMessageMapper.selectFriendPic();
    }

    @Override
    public List<MemberCenter> findMemberCenter() {
        return userMessageMapper.selectMemberCenter();
    }

    @Override
    public RankTab findUserMemberRank(String userId) {
        return userMessageMapper.selectUserMemberRank(userId);
    }


    @Override
    public List<MemberActionInfo> findMemberActionUpInfo() {
        List<MemberActionInfo> actionInfos = productMapper.selectProductStraight();
        return actionInfos;
    }

}
